<?php
/* SVN FILE: $Id: item_othersignature.php 247 2009-10-22 15:31:39Z supersmashbrothers $ */
/**
* ibMarket 2
* IP.Board 3.0
* Change Other's Signature item
*
* @author $Author: supersmashbrothers $
* @copyright $Copyright$
* @version $Revision: 247 $
* @lastrevision $Date: 2009-10-22 15:31:39 +0000 (Thu, 22 Oct 2009) $
* @modifiedby $LastChangedBy: supersmashbrothers $
* @lastmodified $LastChangedDate: 2009-10-22 15:31:39 +0000 (Thu, 22 Oct 2009) $
* @filesource $URL: http://ipb-ibmarket.googlecode.com/svn/ibmarket2/trunk/admin/applications_addon/other/ibmarket/items/other/item_othersignature.php $
*/

class item_othersignature extends templateItem
{
	/**
	 * The default name of the item
	 *
	 * @access	protected
	 * @var		string
	 */
	protected $defaultName = "Change Other Signatures";
	
	/**
	 * The default description of the item
	 *
	 * @access	protected
	 * @var		string
	 */
	protected $defaultDescription = "Use this item to change other member's signatures.";
	
	// Inherited Properties
	/**
	 * Item setting to allow multiple instances within ibMarket 
	 *
	 * @access	protected
	 * @var		boolean
	 */
	protected $allowMultiple = true;
	
	/**
	 * Item specific setting
	 *
	 * @access	protected
	 * @var		array
	 */
	protected $itemSettings = array();
	
	/**
	 * Constructor to setup the item
	 *
	 * @access	public
	 * @param	ipsRegistry	$registry	IPB registry
	 * @param	array		&$item		The item information
	 */
	public function __construct(ipsRegistry $registry, array &$item)
	{
		parent::__construct($registry, $item);
		
		if($item['item_config'] || $item['item_id'] != 0)
		{
			$this->itemSettings = unserialize($item['item_config']);
		}
		else
		{
			$this->itemSettings['protected'] = array($this->settings['admin_group']);
		}
	}
	
	/**
	 * Additional settings when adding/editing the item
	 *
	 * @access	public
	 * @return	string|bool	string if there is something to edit, false otherwise
	 */
	public function adminForm()
	{
		$form['protected'] = ipsRegistry::getClass('output')->generateGroupDropdown('item[protected][]', $this->itemSettings['protected'], true);
		return <<<EOF
				<li>
					<label>{$this->lang->words['groups_protected']}</label>
					{$form['protected']}
				</li>

EOF;
	}
	
	/**
	 * Saving to adding/editing the item
	 *
	 * @access	public
	 * @param	array	&$item_info		The new item information to be stored in the database.
	 * @param	array	[$input]		Relavent input relating to the item
	 * @return	void
	 */
	public function adminSave(array &$item_info, array $input)
	{
		$setting = array();
		$setting['protected'] = is_array($input['protected']) ? $input['protected'] : array();
		
		$item_info['item_config'] = serialize($setting);
	}
	
	/**
	 * When quick saving the price and stock
	 *
	 * @access	public
	 * @param	array	&$item_info		The new item information to be stored in the database.
	 * @param	string	$type			Possible values: 'stockprice', more possibly in the future
	 * @return	void
	 */
	public function adminQuickSave(array &$item_info, $type)
	{
		return;
	}
	
	/**
	 * When removing the item from ibMarket
	 *
	 * @access	public
	 * @return	void
	 */
	public function adminRemove()
	{
		return;
	}
	
	/**
	 * Viewing the file edits if any
	 *
	 * @access	public
	 * @return	string|bool	string if there is something to edit, false otherwise
	 */
	public function adminViewFileEdits()
	{
		return false;
	}
	
	/**
	 * Any admin AJAX functionality
	 *
	 * @access	public
	 * @param	array	$invItem		The inventory item
	 * @return	void
	 */
	public function adminAjax()
	{
		return;
	}
	
	/**
	 * Check and modify the intentory item information before storing in the database
	 *
	 * @access	public
	 * @param	array	&$invItem		The new inventory item information to be stored in the database.
	 * @return	bool	true if the item is successfully created, false otherwise
	 */
	public function publicOnCreateItem(array &$invItem)
	{
		return true;
	}
	
	/**
	 * When using the item
	 * Show a form and validate the form
	 *
	 * @access	public
	 * @param	array	$invItem		The inventory item
	 * @return	string|bool				string if there is a form to be used, true if successfully used, false otherwise
	 */
	public function publicUseItem(array &$invItem)
	{
		$this->registry->getClass('class_localization')->loadLanguageFile( array( 'public_items' ) );
		
		if($this->request['request_method'] == 'post')
		{
			$member = IPSMember::load($this->request['member_name'],'all','displayname');
			if(!$member)
			{
				$this->registry->getClass('output')->showError('no_member');
			}
			
			if(in_array($member['member_group_id'],$this->itemSettings['protected']))
			{
				$this->registry->getClass('output')->showError('protected_member');
			}
			
			$sig_restrictions	= explode( ':', $member['g_signature_limits'] );
			
			if ( (IPSText::mbstrlen($_POST['signature']) > $this->settings['max_sig_length']) and ($this->settings['max_sig_length']) )
			{
				$this->registry->getClass('output')->showError( 'members_sig_too_long' );
			}
			
			$this->request['signature'] = IPSText::removeMacrosFromInput( $this->request['signature'] );
			
			$signature = IPSText::getTextClass( 'editor' )->processRawPost( 'signature' );
			
			IPSText::getTextClass( 'bbcode' )->parse_smilies    = 0;
			IPSText::getTextClass( 'bbcode' )->parse_html       = intval($this->settings['sig_allow_html']);
			IPSText::getTextClass( 'bbcode' )->parse_bbcode     = intval($this->settings['sig_allow_ibc']);
			IPSText::getTextClass( 'bbcode' )->parsing_section	= 'signatures';
			
			$signature		= IPSText::getTextClass('bbcode')->preDbParse( $signature );
			$testSignature	= IPSText::getTextClass('bbcode')->preDisplayParse( $signature );		
			
			if (IPSText::getTextClass( 'bbcode' )->error != "")
			{
				$this->registry->getClass('output')->showError( IPSText::getTextClass( 'bbcode' )->error, 10210 );
			}
			
			$sig_errors	= array();
			
			if( $sig_restrictions[1] !== '' )
			{
				if( substr_count( strtolower($signature), "[img]" ) > $sig_restrictions[1] )
				{
					$sig_errors[] = sprintf( $this->lang->words['sig_toomanyimages'], $sig_restrictions[1] );
				}
			}
			
			if( $sig_restrictions[4] !== '' )
			{
				if( substr_count( strtolower($signature), "[url" ) > $sig_restrictions[4] )
				{
					$sig_errors[] = sprintf( $this->lang->words['sig_toomanyurls'], $sig_restrictions[4] );
				}
				else
				{
					preg_match_all( "#(^|\s|>)((http|https|news|ftp)://\w+[^\s\[\]\<]+)#is", $signature, $matches );
					
					if( count($matches[1]) > $sig_restrictions[4] )
					{
						$sig_errors[] = sprintf( $this->lang->words['sig_toomanyurls'], $sig_restrictions[4] );
					}
				}
			}
			
			if( $sig_restrictions[5] !== '' )
			{
				$testSig	= IPSText::getTextClass( 'bbcode' )->wordWrap( $signature, $this->settings['post_wordwrap'], '<br />' );
				
				if( substr_count( $testSig, "<br />" ) >= $sig_restrictions[5] )
				{
					$sig_errors[] = sprintf( $this->lang->words['sig_toomanylines'], $sig_restrictions[5] );
				}
			}
			
			if( $sig_restrictions[2] !== '' AND $sig_restrictions[3] !== '' )
			{
				preg_match_all( "/\[img\](.+?)\[\/img\]/i", $signature, $allImages );
				
				if( count($allImages[1]) )
				{
					foreach( $allImages[1] as $foundImage )
					{
						$imageProperties = @getimagesize( $foundImage );
						
						if( is_array($imageProperties) AND count($imageProperties) )
						{
							if( $imageProperties[0] > $sig_restrictions[2] OR $imageProperties[1] > $sig_restrictions[3] )
							{
								$sig_errors[] = sprintf( $this->lang->words['sig_imagetoobig'], $foundImage, $sig_restrictions[2], $sig_restrictions[3] );
							}
						}
					}
				}
			}
			
			if( count($sig_errors) )
			{
				$this->registry->getClass('output')->showError( implode( '<br />', $sig_errors ), 10211 );
			}
			
			IPSMember::save( $member['member_id'], array( 'extendedProfile' => array( 'signature' => $signature ) ) );
			
			IPSContentCache::update( $member['member_id'], 'sig', $testSignature );
			
			$this->deleteItems($invItem);
			
			$this->writeLog($invItem, "Changed <a href='{$this->settings['base_url']}showuser={$member['member_id']}'>{$member['members_display_name']}</a> signature");
			
			$this->lang->words['other_member_signature_changed'] = sprintf($this->lang->words['other_member_signature_changed'], $member['members_display_name']);
			$this->registry->getClass('output')->redirectScreen( $this->lang->words['other_member_signature_changed'], $this->settings['base_url_with_app'] . "module=inventory" );
		}
		
		$editor = IPSText::getTextClass('editor')->showEditor( '', 'signature' );
		
		return <<<EOF
<form id='otherSignatureForm' method="post" action="{$this->settings['base_url_with_app']}module=inventory&amp;section=item&amp;item={$invItem['i_id']}">
	<div id='otherSignature'class='block_wrap'>
		<h3 id='otherSignatureHeader' class='maintitle'>{$this->item['item_name']}</h3>
		<fieldset class="row2">
			<ul>
				<li>
					<label>{$this->lang->words['which_member']}</label>
					<input type='text' name='member_name' value='' size='32' id='member_name' autocomplete='off' class='input_text' />
				</li>
				<li>
					{$editor}
				</li>
			</ul>
		</fieldset>
		<fieldset class="submit">
			<input class="input_submit" type="submit" value="{$this->lang->words['change_submit']}" name="submit" />
		</fieldset>
	</div>
</form>
<script type='text/javascript'>
if( $('member_name') )
{
	new ipb.Autocomplete( $('member_name'), { multibox: false, url: ipb.vars['base_url'] + 'app=core&module=ajax&section=findnames&do=get-member-names&secure_key={$this->member->form_hash}&name=', templates: { wrap: ipb.templates['autocomplete_wrap'], item: ipb.templates['autocomplete_item'] } } );
}
</script>
EOF;
	}
	
	/**
	 * Any AJAX functionality
	 *
	 * @access	public
	 * @param	array	$invItem		The inventory item
	 * @return	void
	 */
	public function publicAjax(array &$invItem)
	{
		return;
	}
	
	/**
	 * When the item is being sold back
	 *
	 * @access	public
	 * @param	array	&$invItem	The inventory item information
	 * @return	bool				true if successful, false otherwise
	 */
	public function publicOnResoldItem(array &$invItem)
	{
		return true;
	}
	
	/**
	 * When the item is being restocked
	 *
	 * @access	public
	 * @param	array	&$item	The inventory item information
	 * @return	void
	 */
	public function onItemRestock(array &$item)
	{
		return;
	}
}
